package com.heartware.sh.blackhole.ui.fragment.view;

import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heartware.sh.blackhole.R;
import com.heartware.sh.blackhole.common.utils.ViewUtils;
import com.heartware.sh.blackhole.ui.fragment.NullFragment;

/**
 * Created by Administrator on 2018/3/23 0023.
 */

public class LoadingPager extends FrameLayout {

    public LoadingPager(@NonNull Context context) {
        this(context, null);
    }

    public LoadingPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingPager(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }


    public static final int STATE_UNKNOW = 0;
    public static final int STATE_LOADING = 4;
    public static final int STATE_ERROR = 1;
    public static final int STATE_EMPTY = 2;
    public static final int STATE_SUCCESS = 3;


    private View errorView;
    private View loadingView;
    private View emptyView;
    private View successView;

    private int state = STATE_UNKNOW;



    public void  show(){

        if(state == STATE_ERROR || state == STATE_UNKNOW || state == STATE_EMPTY ){

            state = STATE_LOADING;

        }

        if(state == STATE_LOADING){
            if(loadingListenter != null){
                loadingListenter.load();
            }
        }

        showPage();

    }


    public interface LoadingListenter{

        void load();    //网络请求
        void init();    //初始化successView控件

    }

    private LoadingListenter loadingListenter;

    public void setLoadingListenter(LoadingListenter loadingListenter) {
        this.loadingListenter = loadingListenter;
    }

    public void setState(LoadingResult result) {
        this.state = result.getState();
        showPage();
    }

    public enum LoadingResult{

        EMPTY(STATE_EMPTY), ERROR(STATE_ERROR), UNKNOW(STATE_UNKNOW), SUCCESS(STATE_SUCCESS), LOADING(STATE_LOADING);

        int state;


        LoadingResult(int state) {
            this.state = state;
        }

        public int getState() {
            return state;
        }
    }


    public void init(){

        if(loadingView == null){
            loadingView = createLoadingView();
            this.addView(loadingView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
        }

        if(errorView == null){
            errorView = createErrorView();
            this.addView(errorView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
        }

        if(emptyView == null){
            emptyView = createEmptyView();
            this.addView(emptyView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
        }

    }


    public void showPage(){

        if(loadingView != null){
            loadingView.setVisibility((state == STATE_UNKNOW || state == STATE_LOADING )? View.VISIBLE : View.GONE);
        }

        if(emptyView != null){
            emptyView.setVisibility(state == STATE_EMPTY ? View.VISIBLE : View.GONE);
        }

        if(errorView != null){
            errorView.setVisibility(state == STATE_ERROR ? View.VISIBLE : View.GONE);
            errorView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    show();
                }
            });
        }

        if(state == STATE_SUCCESS && successView != null){

            ViewUtils.removeView(successView);

            this.addView(successView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));

            if(loadingListenter != null){
                loadingListenter.init();
            }
        }


        if(successView != null){
            successView.setVisibility(state == STATE_SUCCESS ? View.VISIBLE : View.GONE);
        }


    }

    private View createLoadingView() {

        TextView tv = new TextView(getContext());
        tv.setText("加载中。。。");
        tv.setTextSize(40);
        return tv;
    }

    private View createEmptyView() {

        TextView tv = new TextView(getContext());
        tv.setText("空数据页面");
        tv.setTextSize(40);

        return tv;

    }

    private View createErrorView() {

        TextView tv = new TextView(getContext());
        tv.setText("请求错误");
        tv.setTextSize(40);

        return tv;
    }


    public void setSuccessView(View successView){
        this.successView = successView;
    }

}
